# -*- coding: utf-8 -*-
import json
from venv import logger

import xlrd
from django.http import HttpResponse
from django.shortcuts import render

from ..models import Interface_Data


def uploadexcel(request):
    if request.method == 'POST':
        f = request.FILES.get('file')
        excel_type = f.name.split('.')[1]
        if excel_type in ['xlsx', 'xls']:
            # 开始解析上传的excel表格
            wb = xlrd.open_workbook(filename=None, file_contents=f.read())
            table = wb.sheets()[0]
            rows = table.nrows  # 总行数
            data_list = []  # 获取表中数据
            try:
                for i in range(rows):  # 遍历每一行数据，将这些数据存储到Interface_Data数据库表中
                    if i != 0:
                        data_list.append(table.row_values(i))
                        in_id = data_list[i - 1][0]
                        in_mname = data_list[i - 1][1]
                        in_type = data_list[i - 1][2]
                        in_url = data_list[i - 1][3]
                        in_data_type = data_list[i - 1][4]
                        in_data = data_list[i - 1][5]
                        in_tname = data_list[i - 1][6]
                        in_expected_result = data_list[i - 1][7]
                        in_actual_result = data_list[i - 1][8]
                        add_interface = Interface_Data(in_id=in_id, in_mname=in_mname, in_type=in_type,
                                                       in_url=in_url,
                                                       in_data_type=in_data_type, in_data=in_data,
                                                       in_tname=in_tname,
                                                       in_expected_result=in_expected_result,
                                                       in_actual_result=in_actual_result,
                                                       )
                        add_interface.save()
                        print(add_interface)
            except:
                logger.error('解析excel文件或者数据插入错误')
            return render(request, 'Page/Interface_List.html', {'message': '导入成功'})
        else:
            logger.error('上传文件类型错误！')
            return render(request, 'Page/Interface_List.html', {'message': '导入失败'})


def pj_uploadexcel(request):
    id = request.POST.get('id')
    if request.method == 'POST':
        f = request.FILES.get('file')
        excel_type = f.name.split('.')[1]
        if excel_type in ['xlsx', 'xls']:  # 判断格式
            # 开始解析上传的excel表格
            wb = xlrd.open_workbook(filename=None, file_contents=f.read())  # 打开指定路径下的excel
            table = wb.sheets()[0]  # 获取表中第一个sheet页
            rows = table.nrows  # 总行数
            data_list = []  # 获取表中数据
            try:
                for i in range(rows):  # 遍历每一行数据，将这些数据存储到Interface_Data数据库表中
                    if i != 0:
                        data_list.append(table.row_values(i))
                        in_id = data_list[i - 1][0]
                        in_mname = data_list[i - 1][1]
                        in_type = data_list[i - 1][2]
                        in_url = data_list[i - 1][3]
                        in_data_type = data_list[i - 1][4]
                        in_data = data_list[i - 1][5]
                        in_tname = data_list[i - 1][6]
                        in_expected_result = data_list[i - 1][7]
                        in_actual_result = data_list[i - 1][8]
                        add_interface = Interface_Data(in_id=in_id, in_mname=in_mname, in_type=in_type,
                                                       in_url=in_url,
                                                       in_data_type=in_data_type, in_data=in_data,
                                                       in_tname=in_tname,
                                                       in_expected_result=in_expected_result,
                                                       in_actual_result=in_actual_result,
                                                       status=id)
                        add_interface.save()

            except:
                logger.error('解析excel文件或者数据插入错误')
            return HttpResponse(json.dumps(data_list), {'message': '导入成功'})
        else:
            logger.error('上传文件类型错误！')
            return render(request, 'Page/Interface_List.html', {'message': '导入失败'})